package com.wanger.swa.convert;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanger.swa.dto.user.req.UserAddReq;
import com.wanger.swa.dto.user.req.UserUpdateReq;
import com.wanger.swa.dto.user.resp.UserResp;
import com.wanger.swa.domain.po.UserPO;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: wanger
 * @Date: 2024/9/16 17:40
 * @Description: 用户
 */
@Component
public class UserConvert {

    private UserPO baseVerify(UserPO po) {
        //进行通用数据验证
        return po;
    }

    public UserPO toSaveEntity(UserAddReq addDTO) {
        return baseVerify(BeanUtil.copyProperties(addDTO, UserPO.class));
    }

    public UserPO toUpdateEntity(UserUpdateReq updateDTO) {
        return baseVerify(BeanUtil.copyProperties(updateDTO, UserPO.class));
    }

    public UserResp toDetailsDTO(UserPO user) {
        return toListDTO(CollUtil.toList(user))
                .stream()
                .findFirst()
                .orElse(null);
    }

    public List<UserResp> toListDTO(List<UserPO> templateList) {
        return BeanUtil.copyToList(templateList, UserResp.class);
    }

    public IPage<UserResp> toPageDTO(IPage<UserPO> page) {
        return new Page<UserResp>()
                .setRecords(toListDTO(page.getRecords()))
                .setTotal(page.getTotal())
                .setSize(page.getSize())
                .setCurrent(page.getCurrent())
                .setPages(page.getPages());
    }
}
